﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework;
using CNVP.Framework.Utils;
using CNVP.JShop.UI;

namespace CNVP.JShop.Web.Admin.CommonWindows
{
    public partial class MenuSet : AdminPage
    {
        public string _ParentID = string.Empty;
        BLL.CNVP_Menu bll = new BLL.CNVP_Menu();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 75)) return;//判断页面权限
            Page.Title = "产品类别管理";
            _ParentID = Request.Params["ParentID"];
            if (!IsPostBack)
            {
                if ((!string.IsNullOrEmpty(_ParentID)) && Public.IsNumber(_ParentID))
                {
                    DataInfo();
                }
                ButtonDelete.Attributes.Add("onclick", "if('" + _ParentID + "'==null){alert('请选择左边节点！');return false;}else{return confirm('确定删除该节点吗?');}");
            }
        }
        private void DataInfo()
        {
            if (_ParentID == null)
            {
                BasePage.MessageBoxUp("请选择左边节点！");
                return;
            }
            Model.CNVP_Menu item = new BLL.CNVP_Menu().GetSingle_CNVP_MenuById(int.Parse(_ParentID));
            if (item != null)
            {
                this.txtID.Text = _ParentID;
                this.txtDepth.Text = item.Depth.ToString();
                this.txtMenuName.Text = item.MenuName;
                this.txtMenuUrl.Text = item.MenuUrl;
                this.txtWeight.Text = item.Weight.ToString();
                this.lbSelectNodesText.Text = "选中的菜单项(节点):<br/><font color=\"blue\" size=\"5\">" + item.MenuName + "</font>";
                this.lbSelectNodesText.Text += "<font size=\"4\">(" + _ParentID + ")</font>";
            }
            else
            {
                BasePage.MessageBoxUp("节点不存在，刷新左边！");
            }
        }
        protected void ButtonAddTopMenu_Click(object sender, EventArgs e)
        {
            this.lbSelectNodesText.Text = "";
            this.txtID.Text = "";
            this.txtDepth.Text = "0";
            this.txtMenuName.Text = "";
            this.txtMenuUrl.Text = "";
            this.txtWeight.Text = "0";
        }

        protected void ButtonAddSubMenu_Click(object sender, EventArgs e)
        {
            foreach (Control c in this.Form.Controls)
            {
                if (c is TextBox)
                {
                    (c as TextBox).Text = "";
                }
            }
            if (_ParentID == null)
            {
                _ParentID = "0";
            }
            this.txtDepth.Text = _ParentID;
            this.txtWeight.Text = "0";
        }

        protected void ButtonDelete_Click(object sender, EventArgs e)
        {
            if (_ParentID == null)
            {
                BasePage.MessageBoxUp("请先选择左边节点！");
                return;
            }
            int CurrentNodeValue = int.Parse(_ParentID);
            var Menu_List = new BLL.CNVP_Menu().GetAll_CNVP_Menu("Depth =" + CurrentNodeValue + "");
            if (Menu_List.Count > 0)
            {
                BasePage.MessageBoxUp("该节点存在子节点,不允许删除.！");
            }
            else
            {
                int rows = new BLL.CNVP_Menu().Delete_CNVP_Menu(int.Parse(_ParentID));
                if (rows == 1)
                {
                    BasePage.MessageBoxUp("删除成功！");
                    this.lbSelectNodesText.Text = "";
                    this.txtID.Text = "";
                    this.txtDepth.Text = "0";
                    this.txtMenuName.Text = "";
                    this.txtMenuUrl.Text = "";
                    this.txtWeight.Text = "0";
                }
            }
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            if (txtID.Text.Trim() == "")
            {
                Model.CNVP_Menu model = new Model.CNVP_Menu();
                model.Depth = txtDepth.Text.Trim();
                model.MenuName = txtMenuName.Text.Trim();
                model.MenuUrl = txtMenuUrl.Text.Trim();
                model.Weight = int.Parse(txtWeight.Text.Trim());
                bll.Add_CNVP_Menu(model);
                BasePage.MessageBoxUp("添加成功！");
            }
            else
            {
                Model.CNVP_Menu model = new Model.CNVP_Menu();
                model.ID = int.Parse(this.txtID.Text);
                model.Depth = txtDepth.Text.Trim();
                model.MenuName = txtMenuName.Text.Trim();
                model.MenuUrl = txtMenuUrl.Text.Trim();
                model.Weight = int.Parse(txtWeight.Text.Trim());
                bll.Update_CNVP_Menu(model);
                BasePage.MessageBoxUp("修改成功！");
            }
        }
    }
}